<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
  String path = request.getContextPath();
%>
<jsp:include page="/pages/commons/top.jsp" />
<jsp:include page="/pages/commons/left.jsp" />

<div data-options="region:'center',title:'字典管理',iconCls:'icon-ok'">  
	<table id="dg" title="字典列表" class="easyui-datagrid" style="width: 1000px; height: 480px" url="<%=path %>/dictionary/getList.do" toolbar="#toolbar" fitColumns="true" method="get" singleSelect="false" loadMsg="数据载入中...">
		<thead>
			<tr>
			    <th data-options="field:'ck',checkbox:true"></th> 
				<th field="id" width="50">流水号</th>
				<th field="dictTypeName" width="50">字典类型</th>
				<th field="dictName" width="50">字典名称</th>
				<th field="dictValue" width="50">字典值</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="padding:5px;height:auto">  
        <div style="margin-bottom:5px">  
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newDictionary()">添加字典</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editDictionary()">修改字典</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delDictionary()">删除字典</a>
		</div>
		<div>  
            字典类型：<input id="queryDictTypeObj" name="queryDictType" class="easyui-combobox" valueField="dictType" textField="dictTypeName"/>  
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="queryDictionary()">查询</a>  
        </div>
	</div>

	<div id="dlg" class="easyui-dialog" style="width: 400px; height: 280px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<div class="ftitle">字典信息</div>
		<form id="fm" method="post" novalidate>
			<div class="fitem">
				<label>字典类型:</label> <input id="dictTypeObj" name="dictType" class="easyui-combobox" required="true" valueField="dictType" textField="dictTypeName"/>
			</div>
			<div class="fitem">
				<label>字典名称:</label> <input id="dictNameObj" name="dictName" class="easyui-validatebox" required="true">
			</div>
			<div class="fitem">
				<label>字典值:</label> <input id="dictValueObj" name="dictValue" class="easyui-validatebox" required="true">
			</div>
			<input id="dictTypeNameObj" name="dictTypeName" value="" type="hidden" /> <input name="id" value="" type="hidden" />

		</form>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveDictionary()">保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
</div>

<jsp:include page="/pages/commons/footer.jsp" />


<script type="text/javascript">  
        var url;  
        function newDictionary(){  
            $('#dlg').dialog('open').dialog('setTitle','添加字典');  
            $('#fm').form('clear'); 
            url = "<%=path%>/dictionary/add.do";  
            $.post("<%=path%>/dictionary/getDictTypeList.do",null,function(result){
            	dictTypeAddOption(null,result);
            });
            
        } 

        function dictTypeAddOption(row,result){
		    var jsonValue = eval(result);
		    $("#dictTypeObj").combobox("clear");
			$("#dictTypeObj").combobox("loadData",jsonValue);
			if(row != null){
			    $("#dictTypeObj").combobox("select",row.dictType);
			}
        }		
        

        function editDictionary(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                $('#dlg').dialog('open').dialog('setTitle','修改字典');  
                $('#fm').form('load',row);
                url = "<%=path%>/dictionary/mod.do?id="+row.id; 
                $.post("<%=path%>/dictionary/getDictTypeList.do",null,function(result){
                	dictTypeAddOption(row,result);
                });
                 
            }  
        }  
        function saveDictionary(){ 
			var dictType = $("#dictTypeObj").combobox('getValue');
            var dictName = $("#dictNameObj").val();
            var dictValue = $("#dictValueObj").val();
            var dictTypeName = $("#dictTypeObj").combobox('getText');
			if($('#fm').form('validate')){
			    var data = "dictType="+dictType+"&dictName="+dictName+"&dictValue="+dictValue+"&dictTypeName="+dictTypeName;
			    $.post(url,data,saveCallBack);  
			}
        } 

        function saveCallBack(result){
            var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
            }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dlg').dialog('close');      // close the dialog  
                $('#dg').datagrid('reload');
    	        return true;
            } 
        }	
 
        function delDictionary(){  
            var row = $('#dg').datagrid('getSelections');  
            if (row.length > 0){  
                $.messager.confirm('Confirm','确定删除该字典吗?',function(r){  
                    if (r){  
						var data = "";
						for(var i=0;i<row.length;i++){
						    data += row[i].id
						    if(i<row.length - 1){
						        data+=",";
							}
						}
                    	$.post("<%=path%>/dictionary/del.do",{'ids':data},delCallBack);   
                    }  
                });  
            }  
        } 
        
        function delCallBack(result){
        	var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
	        }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dg').datagrid('reload');
    	        return true;
            } 
        } 
        
        $(document).ready(
           function(){ 
        	$.post("<%=path%>/dictionary/getDictTypeList.do",null,function(result){
        		var jsonValue = eval(result);
    		    $("#queryDictTypeObj").combobox("clear");
    			$("#queryDictTypeObj").combobox("loadData",jsonValue);
            });
        });
        
        function queryDictionary(){
        	$('#dg').datagrid({url:"<%=path %>/dictionary/getList.do?dictType="+$('#queryDictTypeObj').combobox('getValue')});
        	$('#dg').datagrid('load');
        }

    </script>
<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>

</html>
